package online.lmds.sdmanage.main.device.service;

import online.lmds.sdmanage.common.Status;
import online.lmds.sdmanage.common.entity.Page;
import online.lmds.sdmanage.common.entity.Result;
import online.lmds.sdmanage.main.authentication.dao.UserDao;
import online.lmds.sdmanage.main.authentication.entity.User;

import online.lmds.sdmanage.main.device.dao.DeviceDao;
import online.lmds.sdmanage.main.device.entity.Device;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * Created by Brighten Ma on 2017/4/30.
 */

@Service("DeviceService")
public class DeviceServiceImpl implements DeviceService{

    @Resource(name = "DeviceDao")
    private DeviceDao deviceDao;

    @Resource(name = "UserDao")
    private UserDao userDao;
    @Override
    public Result save(Device device) {
        Result result = new Result();
        if (device == null || device.getUserId() == null){
            return result.setErrorCode().setStatus(Status.BASE_USER_ID_ERROR);
        }
        User user = new User();
        user.setId(device.getUserId());
        User getUser = userDao.getById(user);
        if (getUser == null || getUser.getId() == null) {
            return result.setErrorCode().setStatus(Status.BASE_USER_ID_ERROR);
        }

        //若Android端没有生产uid 添加随机UID
        if (device.getUid().isEmpty()){
            device.setUid(UUID.randomUUID().toString());
        }
        deviceDao.save(device);
        if (device.getId() > 0){
            return getById(device);
        } else {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("添加失败");
        }
    }

    @Override
    public Result del(Device device) {
        Result result = new Result();
        if (device == null || device.getUserId() == null){
            return result.setErrorCode().setStatus(Status.BASE_USER_ID_ERROR);
        }

        User user = new User();
        user.setId(device.getUserId());
        User getUser = userDao.getById(user);
        if (getUser == null || getUser.getId() == null) {
            return result.setErrorCode().setStatus(Status.BASE_USER_ID_ERROR);
        }

        deviceDao.deleteByUid(device);

        return result.setSuccessCode().setStatus(Status.SUCCEED);
    }

    @Override
    public Result getById(Device device) {
        Result result = new Result();
        if (device == null || device.getId() == null){
            return result.setErrorCode().setStatus(Status.BASE_ID_ERROR);
        }

        Device getDevice = deviceDao.getById(device);
        if (getDevice == null || getDevice.getId() == null) {
            return result.setErrorCode().setMessage("未找到指定id设备").setStatus(Status.BASE_ID_ERROR);
        }
        return result.setSuccessCode().setStatus(Status.SUCCEED).setData(getDevice);
    }

    @Override
    public Result getByUid(Device device) {
        Result result = new Result();
        if (device == null || device.getUid() == null){
            return result.setErrorCode().setStatus(Status.BASE_UID_ERROR);
        }

        Device getDevice = deviceDao.getByUid(device);
        if (getDevice == null || getDevice.getUid() == null) {
            return result.setErrorCode().setMessage("未找到指定uid设备").setStatus(Status.BASE_UID_ERROR);
        }
        return result.setSuccessCode().setStatus(Status.SUCCEED).setData(getDevice);
    }

    @Override
    public Result listByUserId(Device device, Page page) {
        Result result = new Result();
        if (device == null || device.getUserId() == null) {
            return result.setErrorCode().setMessage("无效的用户id").setStatus(Status.BASE_ID_ERROR);
        }

        List<Device> getList = deviceDao.list(device, page);
        return result.setSuccessCode().setStatus(Status.SUCCEED).setData(getList);
    }

    @Override
    public Result updateDeviceById(Device device) {
        Result result = new Result();
        if (device == null || device.getId() == null){
            return result.setErrorCode().setStatus(Status.BASE_ID_ERROR);
        }

        int rows = deviceDao.updateById(device);
        if (rows != 1) {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("更新设备失败");
        } else {
            return result.setSuccessCode().setStatus(Status.SUCCEED);
        }
    }

    @Override
    public Result updateDeviceByUid(Device device) {
        Result result = new Result();
        if (device == null || device.getUid() == null){
            return result.setErrorCode().setStatus(Status.BASE_UID_ERROR);
        }

        int rows = deviceDao.updateByUid(device);
        if (rows != 1) {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("更新设备失败");
        } else {
            return result.setSuccessCode().setStatus(Status.SUCCEED);
        }
    }

    @Override
    public Result updateStatusById(Device device) {
        Result result = new Result();
        if (device == null || device.getId() == null){
            return result.setErrorCode().setStatus(Status.BASE_ID_ERROR);
        }

        int rows = deviceDao.updateStatusById(device);
        if (rows != 1) {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("更新设备状态失败");
        } else {
            return result.setSuccessCode().setStatus(Status.SUCCEED);
        }
    }

    @Override
    public Result updateStatusByUid(Device device) {
        Result result = new Result();
        if (device == null || device.getUid() == null){
            return result.setErrorCode().setStatus(Status.BASE_UID_ERROR);
        }

        int rows = deviceDao.updateStatusByUid(device);
        if (rows != 1) {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("更新设备状态失败");
        } else {
            return result.setSuccessCode().setStatus(Status.SUCCEED);
        }
    }



}
